import api from "../../../api/index";
import {setCurrent} from "../../../common/js/util";
export default {
  state:{
    loading:false,
    all_kpi:0,
    recharge_day:0,
    monthSum:0,
    logon_num:0,
    registerCount:0,
    yesterday_recharge_day:0,
    yesterday_login_peo:0,
    yesterday_regist_peo:0,
    echartsData:[],
    tableData:[]
  },
  getters:{
    allGame_recharge_day_inc: state=>{
      return state.yesterday_recharge_day?((state.recharge_day-state.yesterday_recharge_day)*100/state.yesterday_recharge_day).toFixed(2):0;
    },
    allGame_monthSum_inc: state=>{
      return state.all_kpi?(state.monthSum*100/state.all_kpi).toFixed(2):0;
    },
    allGame_logon_num_inc: state=>{
      return state.yesterday_login_peo?((state.logon_num-state.yesterday_login_peo)*100/state.yesterday_login_peo).toFixed(2):0;
    },
    allGame_registerCount_inc: state=>{
      return state.yesterday_regist_peo?((state.registerCount-state.yesterday_regist_peo)*100/state.yesterday_regist_peo).toFixed(2):0;
    },
    allGame_echarts:state=>{
      let regist_peo = [];
      let recharge_day = [];
      let login_peo = [];
      let lineData = [];
      let len = state.echartsData.reduce((res,prs)=>{
        return prs.length>= res?prs.length:res;
      },0);
      let list = state.echartsData.find(item=>item.length===len);
      list&&list.forEach((listitem)=>{
        let s1={value:[]},s2={value:[]},s3 = {value:[]};
        let regis = 0;
        let recharge = 0;
        let login = 0;
        let time = listitem.create_time;
        state.echartsData.forEach((item)=>{
          let s = item.find(it=>it.create_time===time);
          if(s){
            regis+=s.regist_peo;
            recharge +=s.recharge_day;
            login+=s.login_peo;
          }
        });
        s1.value[0] = time;
        s1.value[1] = regis;
        s2.value[0] = time;
        s2.value[1] = recharge
        s3.value[0] = time;
        s3.value[1] = login;
        regist_peo.push(s1);
        recharge_day.push(s2);
        login_peo.push(s3);
      });
      lineData.push(recharge_day);
      lineData.push(login_peo);
      lineData.push(regist_peo);
      return lineData;
    }
  },

  mutations:{
    allGame_tableData(state,data){
      const rowIndex = state.tableData.findIndex(item=>data.project===item.project);
      const  row = state.tableData[rowIndex];
      state.tableData.splice(rowIndex, 1, Object.assign(data,row));
      state.recharge_day=state.tableData.reduce((p,c)=>{
        return p + 100*Number(c.recharge_day)||0;
      },0)/100;
      state.monthSum=state.tableData.reduce((p,c)=>{
        return p + 100*Number(c.monthSum)||0;
      },0)/100;
      state.logon_num=state.tableData.reduce((p,c)=>{
        return p + c.logon_num||0;
      },0);
      state.registerCount=state.tableData.reduce((p,c)=>{
        return p + c.registerCount||0;
      },0);
      state.yesterday_recharge_day=state.tableData.reduce((p,c)=>{
        return p + 100*Number(c.yesterday_recharge_day)||0;
      },0)/100;
      state.yesterday_login_peo=state.tableData.reduce((p,c)=>{
        return p + c.yesterday_login_peo||0;
      },0);
      state.yesterday_regist_peo=state.tableData.reduce((p,c)=>{
        return p + c.yesterday_regist_peo||0;
      },0);
      state.all_kpi = state.tableData.reduce((p,c)=>{
        return p + Number(c.kpi)||0;
      },0);
    },
    allGame_init_tableData(state,list){
      state.tableData = list;
    },
    allGame_init_echartsData(state){
      state.echartsData = [];
    },
    allGame_showLoading(state){
      state.loading = true;
    },
    allGame_endLoading(state){
      state.loading = false;
    },
    allGame_echartsData(state,data){
      !state.echartsData.find(item=>data.project===item.project)&&state.echartsData.push(data);
    }
  },
  actions:{
    allGame_tableData(context,list){
      context.commit('allGame_init_tableData',list);
      //循环发送ajax请求游戏数据
      list.forEach((item)=>{
        api.getArpuCount(item)
          .then((response) =>{
            let data = response.data;
            //判断数据是否正确
            if(data && data!=="erro"){
              data.project_name=item.project_name;
              data.project=item.project;
              data.kpi = item.kpi || 0;
              // setCurrent(data);
              // data.monthSum =(data.monthSum*data.currency.referRate).toFixed(2);
              // data.new_role_recharge_sum =(data.new_role_recharge_sum*data.currency.referRate).toFixed(2);
              // data.recharge_day =(data.recharge_day*data.currency.referRate).toFixed(2);
              // data.recharge_sum =(data.recharge_sum*data.currency.referRate).toFixed(2);
              // data.yesterday_recharge_day =(data.yesterday_recharge_day*data.currency.referRate).toFixed(2);
              context.commit('allGame_tableData', data);
            }

          })
          .catch((error)=> {
            console.log(error);
          });
      });
    },
    allGame_echartsData(context,list){
      context.commit('allGame_init_echartsData');
      list.forEach((item)=>{
        api.getHourCount(item)
          .then((response) =>{
            let data = response.data;
            if(data && data!=="erro"){
              data.project=item.project;
              // data = setCurrent(data);
              // data.forEach(it=>{
              //   it.recharge_day =(it.recharge_day*data.currency.referRate).toFixed(2);
              // });
              //console.log(data);
              context.commit('allGame_echartsData', data);
            }
          })
          .catch((error)=> {
            console.log(error);
          });
      });
    }
  }
}
